package com.css.Thread;
//线程不安全示例2
public class Syn02 {
    public static void main(String[] args) {
        Account account=new Account(100,"基金");
        Drawing you=new Drawing(account,50,"css");

        Drawing dd=new Drawing(account,100,"dd");
        you.start();
        dd.start();

    }
}
class Account{
    int money;//余额
    String name;//卡号

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}
//银行，模拟取款
class Drawing extends Thread{
    Account account;//账户
    int drawingMoney;//去了多少钱
    String drawName;//手里有多少钱
    int nowMoney;
    public Drawing(Account account,int drawingMoney,String drawName){
        super(drawName);
        this.account=account;
        this.drawingMoney=drawingMoney;

    }
    @Override
    public  void run() {
        synchronized (account){
            if(account.money-drawingMoney<0){
                System.out.println("钱不够");
                return;
            }
            //sleep 放大问题的发生性
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //卡内余额
            account.money=account.money-drawingMoney;
            //手里的钱
            nowMoney=nowMoney+drawingMoney;

            System.out.println("手里的钱"+"->"+this.getName()+"->"+nowMoney);
            System.out.println("余额为"+account.money);
        }

    }
}

